package com.frxuexi.mdm_2207.myview

import android.animation.ObjectAnimator
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.compose.ui.unit.sp
import androidx.navigation.NavHostController
import com.tencent.mmkv.MMKV
import kotlinx.coroutines.runBlocking

class MyProgressView: View{
    lateinit var paint_hu:Paint
    lateinit var paint_tx:Paint

     var progress:Float=0f

    var num=5
    lateinit var navHostController:NavHostController
    constructor(context: Context?,navHostController: NavHostController) : super(context){
        initPaint()
        this.navHostController=navHostController
    }
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)


    fun initPaint(){
        paint_hu= Paint()
        paint_hu.color=Color.DKGRAY
        paint_hu.strokeWidth=6f
        paint_hu.style=Paint.Style.STROKE

        paint_tx= Paint()
        paint_tx.color=Color.RED
        paint_tx.textSize=28f

    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        canvas.drawArc(50f,50f,250f,250f,0f,progress,false,paint_hu)
        canvas.drawText("$num",145f,152f,paint_tx)
//        canvas.drawRect(100f,300f,200f,500f,paint_hu)
//        canvas.drawCircle(300f,300f,50f,paint_hu)
//        canvas.drawLine(100f,400f,200f,500f,paint_hu)
//        canvas.drawPoint(400f,600f,paint_hu)
    }

    fun start(){
        runBlocking {
            val animator = ObjectAnimator.ofFloat(0f, 360f)
            animator.addUpdateListener {
                progress=it.animatedValue as Float
                invalidate()
            }
            animator.duration=5*1000L
            animator.start()
        }
        runBlocking {
            val animator = ObjectAnimator.ofInt(num, 1)
            animator.addUpdateListener {
                num=it.animatedValue as Int
                invalidate()
                if(num==1){
                    //存储当前时间
                MMKV.defaultMMKV().encode("pre",System.currentTimeMillis())
                navHostController.navigate("main")
                }
            }
            animator.duration=5*1000L
            animator.start()
        }
    }

}